package test.multithread;


public class Common {
	static Object obj = new Object();
	static Object proObj = new Object();
	static Object conObj = new Object();
	static Buf buf = new Buf();

	public static void main(String[] args) {

	}

}

class Pro extends Thread {
	public void run() {
		int i = 0;
		while (i++ < 100) {
			synchronized (Common.obj) {
				if (Common.buf.isFull()) {
					try {
						Common.proObj.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				Common.buf.put();
				Common.conObj.notify();
				try {
					sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}

}

class Cons extends Thread{
	public void run() {
		int i = 0;
		while (i++ < 100) {
			synchronized (Common.obj) {
				if (Common.buf.isEmpty()) {
					try {
						Common.conObj.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				Common.buf.get();
				Common.proObj.notify();
				try {
					sleep(10);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}

class Buf {
	final int MAX = 10;
	int[] buf = new int[MAX];
	int current = 0;
	int i;

	boolean isFull() {
		if (current >= MAX)
			return true;
		return false;
	}

	boolean isEmpty() {
		if (current <= 0)
			return true;
		return false;
	}

	void put() {
		buf[current++] = i++;
		System.out.println("              " + Thread.currentThread().getName()
				+ i);
	}

	void get() {
		System.out.println(Thread.currentThread().getName() + buf[--current]);
	}

}